﻿#ifndef _TES_MATRIX3X3_H_
#define _TES_MATRIX3X3_H_

#include <math.h>


#ifndef PAI
#define PAI 3.1415926585f
#endif

class TesEular;
class TesVector2;
class TesVector3;
class TesMatrix2x2;
class TesMatrix4x4;
class TesQuaternion;

class TesMatrix3x3
{
public:
	float m3x3[9];

	void Zero();
	TesMatrix3x3();
	TesMatrix3x3(float m[9]);
	void SetMatrix(float m[9]); 
	void SetMatrix(const TesMatrix2x2& matrix2x2);
	TesMatrix3x3(const TesMatrix2x2& matrix2x2);
	TesMatrix3x3& operator = (const TesMatrix3x3& that);
	void Identity();
	TesMatrix3x3 operator + (const TesMatrix3x3& that);
	TesMatrix3x3 operator - (const TesMatrix3x3& that);
	TesMatrix3x3 operator * (const TesMatrix3x3& that);
	TesMatrix3x3 operator * (const float& K) const;
	TesMatrix3x3 operator / (const float& K); 
	float _Cofactor(int r, int c); //代数余子式
	TesMatrix3x3 _AdjoinMatrix(); //伴随矩阵
	float Determinant();  //行列式
	TesMatrix3x3 Transpose(); //转置
	TesMatrix3x3 Inverse(); //逆
	TesMatrix3x3 SetScale(float x, float y, float z);
	TesMatrix3x3 SetScale2D(float x, float y);
	TesMatrix3x3 SetScale2D(const TesMatrix2x2& M2x2);
	TesMatrix3x3 SetRotationXAxis(float Redus);
	TesMatrix3x3 SetRotationYAxis(float Redus);
	TesMatrix3x3 SetRotationZAxis(float Redus);
	TesMatrix3x3 SetRotation(float Redus, const TesVector3& Axis);
	TesMatrix3x3 SetRotation2D(float Redus);
	TesMatrix3x3 SetTranslation2D(float x, float y);
	TesEular Euler();   //但是矩阵必须是旋转矩阵
	TesQuaternion Quaternion();
	void _GetToArray(float matrix[9]);
	TesMatrix4x4 ExtendToMatrix4x4();
	TesMatrix4x4 Transform2DToMatrix4x4();
};

#define PosTOIndex3(x, y) ((y - 1) * 3 + x - 1)
TesVector2 operator * (const TesVector2& v, const TesMatrix3x3& matrix);
TesVector3 operator * (const TesVector3& v, const TesMatrix3x3& matrix);
TesMatrix3x3 operator * (const float& K, const TesMatrix3x3& matrix);
TesMatrix2x2 GetMatrix2x2(const TesMatrix3x3& matrix, int Row, int Column);

#define M11_3 m3x3[0]
#define M12_3 m3x3[1]
#define M13_3 m3x3[2]
#define M21_3 m3x3[3]
#define M22_3 m3x3[4]
#define M23_3 m3x3[5]
#define M31_3 m3x3[6]
#define M32_3 m3x3[7]
#define M33_3 m3x3[8]

#endif